package com.jz.community.modulemine.information.task;

import android.app.Activity;

import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.net.OkHttpUtil;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.task.RxTask;
import com.jz.community.basecomm.utils.JsonUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.commview.view.widget.ProgressDialogManager;
import com.jz.community.modulemine.information.bean.UploadInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Admin on 2018/3/28 0028.
 */

public class UpLoadAvatarTask extends RxTask<String, Integer, UploadInfo> {

    private Activity activity;
    private ITaskCallbackListener taskListener;

    public UpLoadAvatarTask(Activity activity, ITaskCallbackListener taskListener) {
        super(activity);
        this.activity = activity;
        this.taskListener = taskListener;
    }


    @Override
    protected UploadInfo doInBackground(String... params) {
        String result = OkHttpUtil.postFile(Constant.UPDATE_IMAGE_URL, new HashMap<>(), "uploadIcon", params[0]);
        UploadInfo responseInfo = null;
        if (!Preconditions.isNullOrEmpty(result)) {
            responseInfo = JsonUtils.parseObject(result, UploadInfo.class);
        }
        return responseInfo;
    }

    @Override
    protected void onPreExecute() {
        ProgressDialogManager.showDialog(activity);
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(UploadInfo responseInfo) {
        // 执行结果回调函数
        taskListener.doTaskComplete(responseInfo);
        ProgressDialogManager.dismissProgressDialog();
        super.onPostExecute(responseInfo);
    }

}
